﻿using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace BoldBrick.SharePoint.CommonControls.DataObjects.Base
{
	/// <summary>
	/// Base metadata class for <see cref="SPWorkflow"/> object
	/// </summary>
	public class Workflow
	{
		#region Fields

		private readonly string nameResourceKey;
		private readonly string resourceFileName;

		#endregion

		
		#region Properties
		
		/// <summary>
		/// Gets ID of the workflow
		/// </summary>
		public Guid ID { get; private set; }

		/// <summary>
		/// List of workflow history
		/// </summary>
		public BaseList ListWorkflowHistory { get; private set; }

		/// <summary>
		/// List of workflow tasks
		/// </summary>
		public BaseList ListWorkflowTasks { get; private set; }

		public bool AllowManual { get; set; }

		public bool AutoStartCreate { get; set; }

		public bool AutoStartChange { get; set; }

		/// <summary>
		/// ID of content type to which the workflow will be associated
		/// </summary>
		public SPContentTypeId? ContentTypeID { get; set; } 

		#endregion

		#region Constructor

		/// <summary>
		/// Constructor for <see cref="Workflow"/> object
		/// </summary>
		/// <param name="id">ID of the workflow</param>
		/// <param name="resourceFileName">File name of resource of the workflow (without ".cs-cz.resx" extension)</param>
		/// <param name="nameResourceKey">Resource key of name of the workflow</param>
		/// <param name="listWorkflowHistory">List of workflow history</param>
		/// <param name="listWorkflowTasks">List of workflow tasks</param>
		public Workflow(Guid id, string resourceFileName, string nameResourceKey, BaseList listWorkflowHistory, BaseList listWorkflowTasks)
		{
			this.ID = id;
			this.resourceFileName = resourceFileName;
			this.nameResourceKey = nameResourceKey;
			this.ListWorkflowHistory = listWorkflowHistory;
			this.ListWorkflowTasks = listWorkflowTasks;
			
			this.AllowManual = true;
			this.AutoStartCreate = false;
			this.AutoStartChange = false;
		}

		#endregion

		#region Methods
		
		/// <summary>
		/// Gets name of the workflow
		/// </summary>
		/// <param name="language">Language identifier</param>
		/// <returns>Returns name of the workflow</returns>
		public virtual string GetName(uint language)
		{
			string source = string.Format("$Resources: {0}", nameResourceKey);
			return SPUtility.GetLocalizedString(source, resourceFileName, language);
		}

		/// <summary>
		/// Gets state of the workflow associated with the item
		/// </summary>
		/// <param name="site">Parent site of the item</param>
		/// <param name="item">The item which workflow is associated with</param>
		/// <returns>Returns state of the workflow associated with the item</returns>
		public SPWorkflowState GetWorkflowState(SPSite site, SPListItem item)
		{
			SPWorkflowCollection workflows = site.WorkflowManager.GetItemActiveWorkflows(item);
			SPWorkflow workflow = workflows.OfType<SPWorkflow>()
				.FirstOrDefault(w => w.ParentAssociation.BaseTemplate.Id == this.ID);
			if (workflow == null)
				return SPWorkflowState.None;

			return workflow.InternalState;
		}

		#endregion
	}
}
